package org.infodavid.common.model.impl;

import java.util.ArrayList;
import java.util.List;

import org.infodavid.common.model.security.IAccessControlEntry;
import org.infodavid.common.model.security.IAccessControlList;
import org.infodavid.common.model.security.ISecurityIdentifier;

/**
 * The Class AccessControlListImpl.
 */
public class AccessControlListImpl extends AbstractImprovedDataObjectImpl<Long> implements
    IAccessControlList {

	/** serialVersionUID. */
	private static final long serialVersionUID = -1478066595640761601L;

	/** The comment. */
	private String comment;

	/** The entries. */
	private List<IAccessControlEntry> entries;

	/** The identifier. */
	private ISecurityIdentifier identifier;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected AccessControlListImpl() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected AccessControlListImpl(final Long keyArg) throws Exception {
		super();

		setKey(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IAccessControlList)) {
			return false;
		}

		final IAccessControlList other = (IAccessControlList)obj;

		if (getKey() == null) {
			if (other.getKey() != null) {
				return false;
			}
		}
		else if (!getKey().equals(other.getKey())) {
			return false;
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#getComment()
	 */
	@Override
	public String getComment() {
		return comment;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.IAccessControlList#getEntries()
	 */
	@Override
	public List<IAccessControlEntry> getEntries() {
		return entries;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.IAccessControlList#getIdentifier()
	 */
	@Override
	public ISecurityIdentifier getIdentifier() {
		return identifier;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getKey() == null ? 0 : getKey().hashCode());
		result = prime * result + (getIdentifier() == null ? 0 : getIdentifier().hashCode());

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IBasicDataObject#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		comment = null;
		identifier = null;
		entries = new ArrayList<IAccessControlEntry>();

	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#setComment(java.lang.String)
	 */
	@Override
	public void setComment(final String value) {
		if (!equals(comment, value)) {
			final Object old = comment;
			comment = value;
			markModified(COMMENT, old, comment);
		}
	}

	/**
	 * Sets the entries.
	 * @param values the new entries
	 */
	public void setEntries(final List<IAccessControlEntry> values) {
		final Object old = entries;
		entries = values;
		markModified(ENTRIES, old, entries);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.security.IAccessControlList#setIdentifier(org.infodavid.common.model
	 * .security.ISecurityIdentifier)
	 */
	@Override
	public void setIdentifier(final ISecurityIdentifier value) {
		if (!equals(identifier, value)) {
			final Object old = identifier;
			identifier = value;
			markModified(IDENTIFIER, old, identifier);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[comment=");
		builder.append(comment);
		builder.append(", identifier=");
		builder.append(identifier);
		builder.append("]");

		return builder;
	}
}
